
require("Scripts/Common/Class")

EventDispatcher = Class()

function EventDispatcher:Ctor()
	-- 侦听对象
	self._listeners = {}
	-- 是否禁用
	self._abs_disable = false
end

-- 添加侦听对象：侦听事件类型，侦听对象，以及回调函数
function EventDispatcher:AddEventListener(eventtype, object, callback)
	if (self._listeners[eventtype] == nil)
	then
		self._listeners[eventtype] = { }
	end

	self._listeners[eventtype][object] = callback
end

-- 移除侦听对象：侦听事件类型，侦听对象
function EventDispatcher:RemoveEventListener(eventtype, object)
	if (self._listeners[eventtype] == nil) then return end
	self._listeners[eventtype][object] = nil
end

-- 清理所有侦听
function EventDispatcher:Clear()
	self._listeners = { }
end

-- 分发事件
function EventDispatcher:DispatchEvent(event)
	if (self._abs_disable) then return false end
	if (self._listeners[event.type] == nil)then return false end
	local listener = self._listeners[event.type]
	for k, v in pairs(listener)
	do
		k[v](k, event)
	end
end
